package com.ssy.lingxi.report.utils;

import io.micrometer.core.instrument.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {

    /**
     * 获取两个日期之间的所有日期(字符串格式, 按天计算)
     *
     * @param startTime String 开始时间 yyyy-MM-dd
     * @param endTime  String 结束时间 yyyy-MM-dd
     * @return 两个日期之间的所有日期
     */
    public static List<String> getBetweenDays(String startTime, String endTime) {
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return null;
        }
        //1、定义转换格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date start = df.parse(startTime);
            Date end = df.parse(endTime);

            List<String> result = new ArrayList<>();

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            result.add(sdf.format(start));
            while (tempStart.before(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
            if (!result.contains(sdf.format(end))) {
                result.add(sdf.format(end));
            }
            return result;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取两个日期之间的所有日期(字符串格式, 按月计算)
     *
     * @param startTime String 开始时间 yyyy-MM-dd
     * @param endTime  String 结束时间 yyyy-MM-dd
     * @return 两个日期之间的所有日期(字符串格式, 按月计算)
     */
    public static List<String> getBetweenMonths(String startTime, String endTime) {
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return null;
        }
        //1、定义转换格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date start = df.parse(startTime);
            Date end = df.parse(endTime);

            List<String> result = new ArrayList<>();

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            tempStart.add(Calendar.MONTH, 1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            result.add(sdf.format(start));
            while (tempStart.before(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.MONTH, 1);
            }
            if (!result.contains(sdf.format(end))) {
                result.add(sdf.format(end));
            }
            return result;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 将long类型的timestamp转为LocalDateTime
     * @author 伍将
     * @date 2020/11/10
     * @param timestamp:
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * 将LocalDateTime转为long类型的timestamp
     * @author 伍将
     * @date 2020/11/10
     * @param localDateTime:
     * @return long
     **/
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 将某时间字符串转为自定义时间格式的LocalDateTime
     * @author 伍将
     * @date 2020/11/10
     * @param time:
     * @param format:
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    /**
     * 将LocalDateTime转为自定义的时间格式的字符串
     * @author 伍将
     * @date 2020/11/10
     * @param localDateTime:
     * @param format:
     * @return java.lang.String
     **/
    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 获取月份最后一天
     * @author 伍将
     * @date 2020/11/9
     * @param date:
     * @return java.util.Date
     **/
    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }


    /**
     * 获取月份第一天
     * @author 伍将
     * @date 2020/11/9
     * @param date:
     * @return java.util.Date
     **/
    public static Date getMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 0);
        return calendar.getTime();
    }
}
